package com.network.topo.snmp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.snmp4j.Target;

import com.network.util.snmp4j.SnmpGet;
import com.network.util.snmp4j.SnmpGetResultSet;

public final class IpAddrTable extends AbstractXxxTable
{
	private static ArrayList<String> oidList = null;
	private HashMap<Object, RowData> objDataMap = new HashMap<Object, RowData>();
	private HashMap<Object, RowData> indexDataMap = new HashMap<Object, RowData>();
	private int rowCount = 0;

	private IpAddrTable(SnmpGetResultSet resultSet)
	{
		this.resultSet = resultSet;
		if (this.resultSet != null)
		{
			rowCount = this.resultSet.getRowCount();
			for (int i = 0; i < rowCount; i++)
			{
				RowData rowData = new RowData();
				int oidIndex = 0;
				rowData.ipAdEntAddr = getValue(getOidList().get(oidIndex++), i);
				rowData.ipAdEntIfIndex = str2int(getValue(getOidList().get(oidIndex++), i));
				rowData.ipAdEntNetMask = getValue(getOidList().get(oidIndex++), i);
				// rowData.ipAdEntBcastAddr =
				// str2int(getValue(getOidList().get(oidIndex++), i));
				// rowData.ipAdEntReasmMaxSize =
				// str2int(getValue(getOidList().get(oidIndex++), i));
				indexDataMap.put(i, rowData);
				objDataMap.put(rowData.getIpAdEntAddr(), rowData);
				objDataMap.put(rowData.getIpAdEntIfIndex(), rowData);
			}
		}
	}

	public static IpAddrTable getInstance(SnmpGet snmpGet, Target target)
	{
		SnmpGetResultSet sgrs = null;
		IpAddrTable instance = null;
		try
		{
			sgrs = snmpGet.query(getOidList(), target);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		if (sgrs != null && sgrs.next() == true)
		{
			instance = new IpAddrTable(sgrs);
		}
		return instance;
	}

	public static synchronized ArrayList<String> getOidList()
	{
		if (oidList == null)
		{
			oidList = new ArrayList<String>();
			oidList.add("1.3.6.1.2.1.4.20.1.1");
			oidList.add("1.3.6.1.2.1.4.20.1.2");
			oidList.add("1.3.6.1.2.1.4.20.1.3");
			// oidList.add("1.3.6.1.2.1.4.20.1.4");
			// oidList.add("1.3.6.1.2.1.4.20.1.5");
		}
		return oidList;
	}

	public boolean isContains(String ipAddr)
	{
		boolean rtnCode = false;
		if (objDataMap.get(ipAddr) != null)
		{
			rtnCode = true;
		}
		return rtnCode;
	}

	public String getIpAdEntAddr(Object obj, boolean useIndex)
	{
		String rtnValue = "127.0.0.1";
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIpAdEntAddr();
		}
		return rtnValue;
	}

	public int getIpAdEntIfIndex(Object obj, boolean useIndex)
	{
		int rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIpAdEntIfIndex();
		}
		return rtnValue;
	}

	public String getIpAdEntNetMask(Object obj, boolean useIndex)
	{
		String rtnValue = "0.0.0.0";
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIpAdEntNetMask();
		}
		return rtnValue;
	}

	public int getIpAdEntBcastAddr(Object obj, boolean useIndex)
	{
		int rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIpAdEntBcastAddr();
		}
		return rtnValue;
	}

	public int getIpAdEntReasmMaxSize(Object obj, boolean useIndex)
	{
		int rtnValue = 0;
		RowData rowData = null;
		if (useIndex)
		{
			rowData = indexDataMap.get(obj);
		}
		else
		{
			rowData = objDataMap.get(obj);
		}
		if (rowData != null)
		{
			rtnValue = rowData.getIpAdEntReasmMaxSize();
		}
		return rtnValue;
	}
	public class RowData
	{
		private String ipAdEntAddr = "";
		private int ipAdEntIfIndex = 0;
		private String ipAdEntNetMask = "";
		private int ipAdEntBcastAddr = 0;
		private int ipAdEntReasmMaxSize = 0;

		public String getIpAdEntAddr()
		{
			return ipAdEntAddr;
		}

		public void setIpAdEntAddr(String ipAdEntAddr)
		{
			this.ipAdEntAddr = ipAdEntAddr;
			if (this.ipAdEntAddr == null || this.ipAdEntAddr.split("[.]").length != 4)
			{
				this.ipAdEntAddr = "127.0.0.1";
			}
		}

		public int getIpAdEntBcastAddr()
		{
			return ipAdEntBcastAddr;
		}

		public void setIpAdEntBcastAddr(int ipAdEntBcastAddr)
		{
			this.ipAdEntBcastAddr = ipAdEntBcastAddr;
		}

		public int getIpAdEntIfIndex()
		{
			return ipAdEntIfIndex;
		}

		public void setIpAdEntIfIndex(int ipAdEntIfIndex)
		{
			this.ipAdEntIfIndex = ipAdEntIfIndex;
		}

		public String getIpAdEntNetMask()
		{
			return ipAdEntNetMask;
		}

		public void setIpAdEntNetMask(String ipAdEntNetMask)
		{
			this.ipAdEntNetMask = ipAdEntNetMask;
			if (this.ipAdEntNetMask == null || this.ipAdEntNetMask.split("[.]").length != 4)
			{
				this.ipAdEntNetMask = "0.0.0.0";
			}
		}

		public int getIpAdEntReasmMaxSize()
		{
			return ipAdEntReasmMaxSize;
		}

		public void setIpAdEntReasmMaxSize(int ipAdEntReasmMaxSize)
		{
			this.ipAdEntReasmMaxSize = ipAdEntReasmMaxSize;
		}
	}

	public int getRowCount()
	{
		return rowCount;
	}

	public void setRowCount(int rowCount)
	{
		this.rowCount = rowCount;
	}
}
